package cake.web.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cake.dao.CakeDao;
import cake.dao.CategoryDao;
import cake.dao.impl.CakeDaoImpl;
import cake.dao.impl.CategoryDaoImpl;
import cake.entity.Cake;
import cake.entity.Category;


@WebServlet("/admin/cakeEdit.html")
public class AdminCakeEditServlet extends HttpServlet {
	private CakeDao cakeDao=new CakeDaoImpl();
	private CategoryDao categoryDao=new CategoryDaoImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Category> clist=categoryDao.getAllCategory();
		req.setAttribute("clist", clist);
		if(req.getParameter("kid")!=null&&!req.getParameter("kid").isEmpty()) {
			int id = Integer.parseInt(req.getParameter("kid"));
			Cake cake=cakeDao.getCakeById(id);
			req.setAttribute("cake", cake);
		}
		req.getRequestDispatcher("cakeEdit.jsp").forward(req, resp);

	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		FileItemFactory factory=new DiskFileItemFactory();
		ServletFileUpload upload=new ServletFileUpload(factory);
		
		int id=0;
		String name="";
		int categoryid=0;
		float price=0;
		int stock=0;
		String mainimg="";

		try {
			List<FileItem> list= upload.parseRequest(req);
			for (FileItem item : list) {
				if(item.isFormField()) {
					if("id".equals(item.getFieldName())) {
						if(!item.getString("utf-8").isEmpty()) {
						id=Integer.parseInt(item.getString("utf-8"));
						}
					}
					if("name".equals(item.getFieldName())) {
						name=item.getString("utf-8");
					}
					if("cid".equals(item.getFieldName())) {
						categoryid=Integer.parseInt(item.getString("utf-8"));
					}
					if("price".equals(item.getFieldName())) {
						price=Float.parseFloat(item.getString("utf-8"));
					}
					if("stock".equals(item.getFieldName())) {
						stock=Integer.parseInt(item.getString("utf-8"));
					}
					
				}else {
					if(!item.getFieldName().isEmpty()) {
					String ext= item.getName().substring(item.getName().lastIndexOf("."));
					mainimg=System.currentTimeMillis()+ext;
					String url="/image/productImg/"+mainimg;
					String path=req.getServletContext().getRealPath(url);
					item.write(new File(path));
					}
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("上传失败",e);
		}
		Cake cake=new Cake(0, name, categoryid, price, stock, true, mainimg);

		if(id!=0) {
			cakeDao.updateCake(cake, id);
		}else {
			cakeDao.addCake(cake);
		}
		
		resp.sendRedirect("cakelist.html");
	    
		
	}
}
